#include "qmysqldabasemngr.h"
#include <QSqlError>

QMySqlDatabaseManager::QMySqlDatabaseManager() : DatabaseManager("QMYSQL")
{
}

QMySqlDatabaseManager::~QMySqlDatabaseManager()
{
  QSqlDatabase::removeDatabase(QString(database_name.c_str()));
}

bool QMySqlDatabaseManager::openDatabase()
{
  database = QSqlDatabase::addDatabase(QString(database_type.c_str()));
  database.setHostName(QString(host_name.c_str()));
  database.setDatabaseName(QString(database_name.c_str()));
  database.setUserName(QString(user_name.c_str()));
  database.setPassword(QString(user_pswd.c_str()));
  return database.open();
}

QSqlDatabase QMySqlDatabaseManager::getSqlDataBase()
{
  return database;
}

bool QMySqlDatabaseManager::closeDatabase()
{
  database.close();
  return true;
}

QString QMySqlDatabaseManager::getLastError()
{
  QSqlError sql_err = database.lastError();
  QString str_error = QString("&Error: [") + QString::number(sql_err.number()) + QString("] ") + sql_err.text();
  return str_error;
}
